package com.starhub.application.agent.concurrent;

import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

@Component
public class MemoryConcurrencyCounter implements ConcurrencyCounter {
    private final Map<Long, AtomicInteger> agentCounts = new ConcurrentHashMap<>();
    private final AtomicInteger totalCount = new AtomicInteger(0);

    @Override
    public void incrementAgentCount(Long agentId) {
        agentCounts.computeIfAbsent(agentId, k -> new AtomicInteger(0)).incrementAndGet();
    }

    @Override
    public void decrementAgentCount(Long agentId) {
        AtomicInteger count = agentCounts.get(agentId);
        if (count != null) {
            count.decrementAndGet();
        }
    }

    @Override
    public int getAgentCount(Long agentId) {
        AtomicInteger count = agentCounts.get(agentId);
        return count != null ? count.get() : 0;
    }

    @Override
    public void incrementTotalCount() {
        totalCount.incrementAndGet();
    }

    @Override
    public void decrementTotalCount() {
        totalCount.decrementAndGet();
    }

    @Override
    public int getTotalCount() {
        return totalCount.get();
    }
} 